package com.example.socialapp.ui.settings

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ChevronRight
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.PrivacyTip
import androidx.compose.material.icons.filled.Storage
import androidx.compose.material.icons.outlined.AccountCircle
import androidx.compose.material.icons.outlined.CloudUpload
import androidx.compose.material.icons.outlined.DarkMode
import androidx.compose.material.icons.outlined.Devices
import androidx.compose.material.icons.outlined.HeadsetMic
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material.icons.outlined.Key
import androidx.compose.material.icons.outlined.Language
import androidx.compose.material.icons.outlined.Lock
import androidx.compose.material.icons.outlined.Notifications
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp

/**
 * 设置界面
 *
 * @param onNavigateBack 返回上一界面回调
 * @param onNavigateToAccountSettings 导航到账户设置回调
 * @param onNavigateToPrivacySettings 导航到隐私设置回调
 * @param onNavigateToNotificationSettings 导航到通知设置回调
 * @param onNavigateToThemeCustomization 导航到主题自定义回调
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(
    onNavigateBack: () -> Unit,
    onNavigateToAccountSettings: () -> Unit = {},
    onNavigateToPrivacySettings: () -> Unit = {},
    onNavigateToNotificationSettings: () -> Unit = {},
    onNavigateToThemeCustomization: () -> Unit = {}
) {
    val scrollState = rememberScrollState()
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("设置") },
                navigationIcon = {
                    IconButton(onClick = onNavigateBack) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack,
                            contentDescription = "返回"
                        )
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
                )
            )
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
                .verticalScroll(scrollState)
        ) {
            // 账户设置
            SettingsSection(
                title = "账户设置",
                items = listOf(
                    SettingsItem.Simple(
                        title = "个人资料",
                        icon = Icons.Outlined.AccountCircle,
                        onClick = onNavigateToAccountSettings
                    ),
                    SettingsItem.Simple(
                        title = "账号安全",
                        icon = Icons.Outlined.Lock,
                        onClick = {}
                    ),
                    SettingsItem.Simple(
                        title = "密码修改",
                        icon = Icons.Outlined.Key,
                        onClick = {}
                    )
                )
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 隐私设置
            SettingsSection(
                title = "隐私设置",
                items = listOf(
                    SettingsItem.Simple(
                        title = "隐私权限",
                        icon = Icons.Default.PrivacyTip,
                        onClick = onNavigateToPrivacySettings
                    ),
                    SettingsItem.Toggleable(
                        title = "位置分享",
                        icon = Icons.Outlined.Devices,
                        initialValue = false
                    ),
                    SettingsItem.Toggleable(
                        title = "在线状态显示",
                        icon = Icons.Outlined.CloudUpload,
                        initialValue = true
                    )
                )
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 通知设置
            SettingsSection(
                title = "通知设置",
                items = listOf(
                    SettingsItem.Simple(
                        title = "通知管理",
                        icon = Icons.Default.Notifications,
                        onClick = onNavigateToNotificationSettings
                    ),
                    SettingsItem.Toggleable(
                        title = "消息提醒",
                        icon = Icons.Outlined.Notifications,
                        initialValue = true
                    ),
                )
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 系统设置
            SettingsSection(
                title = "系统设置",
                items = listOf(
                    SettingsItem.Simple(
                        title = "主题与外观",
                        icon = Icons.Outlined.DarkMode,
                        subtitle = "自定义应用主题",
                        onClick = onNavigateToThemeCustomization
                    ),
                    SettingsItem.Simple(
                        title = "语言选择",
                        icon = Icons.Outlined.Language,
                        subtitle = "简体中文",
                        onClick = {}
                    ),
                    SettingsItem.Simple(
                        title = "清除缓存",
                        icon = Icons.Default.Storage,
                        subtitle = "45.8 MB",
                        onClick = {}
                    )
                )
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 关于与帮助
            SettingsSection(
                title = "关于与帮助",
                items = listOf(
                    SettingsItem.Simple(
                        title = "帮助中心",
                        icon = Icons.Outlined.HeadsetMic,
                        onClick = {}
                    ),
                    SettingsItem.Simple(
                        title = "关于我们",
                        icon = Icons.Outlined.Info,
                        subtitle = "版本 1.0.0",
                        onClick = {}
                    )
                )
            )
            
            Spacer(modifier = Modifier.height(32.dp))
        }
    }
}

/**
 * 设置分区
 *
 * @param title 分区标题
 * @param items 设置项列表
 */
@Composable
private fun SettingsSection(
    title: String,
    items: List<SettingsItem>
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp)
    ) {
        Text(
            text = title,
            style = MaterialTheme.typography.titleMedium,
            color = MaterialTheme.colorScheme.primary,
            modifier = Modifier.padding(vertical = 8.dp)
        )
        
        Card(
            shape = RoundedCornerShape(12.dp),
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface
            ),
            elevation = CardDefaults.cardElevation(defaultElevation = 1.dp)
        ) {
            Column {
                items.forEachIndexed { index, item ->
                    when (item) {
                        is SettingsItem.Simple -> SimpleSettingsItem(item)
                        is SettingsItem.Toggleable -> ToggleableSettingsItem(item)
                    }
                    
                    if (index < items.size - 1) {
                        Divider(
                            modifier = Modifier.padding(start = 56.dp, end = 16.dp),
                            color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f)
                        )
                    }
                }
            }
        }
    }
}

/**
 * 简单设置项（点击类型）
 *
 * @param item 设置项数据
 */
@Composable
private fun SimpleSettingsItem(item: SettingsItem.Simple) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = item.onClick)
            .padding(vertical = 12.dp, horizontal = 16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            imageVector = item.icon,
            contentDescription = item.title,
            tint = MaterialTheme.colorScheme.primary
        )
        
        Spacer(modifier = Modifier.width(16.dp))
        
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = item.title,
                style = MaterialTheme.typography.bodyLarge
            )
            
            if (item.subtitle != null) {
                Text(
                    text = item.subtitle,
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.outline
                )
            }
        }
        
        Icon(
            imageVector = Icons.Default.ChevronRight,
            contentDescription = null,
            tint = MaterialTheme.colorScheme.outline
        )
    }
}

/**
 * 可切换设置项（开关类型）
 *
 * @param item 设置项数据
 */
@Composable
private fun ToggleableSettingsItem(item: SettingsItem.Toggleable) {
    var isChecked by remember { mutableStateOf(item.initialValue) }
    
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable { isChecked = !isChecked }
            .padding(vertical = 12.dp, horizontal = 16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            imageVector = item.icon,
            contentDescription = item.title,
            tint = MaterialTheme.colorScheme.primary
        )
        
        Spacer(modifier = Modifier.width(16.dp))
        
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = item.title,
                style = MaterialTheme.typography.bodyLarge
            )
            
            if (item.subtitle != null) {
                Text(
                    text = item.subtitle,
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.outline
                )
            }
        }
        
        Switch(
            checked = isChecked,
            onCheckedChange = { isChecked = it }
        )
    }
}

/**
 * 设置项密封类
 */
sealed class SettingsItem {
    /**
     * 简单设置项（点击类型）
     */
    data class Simple(
        val title: String,
        val icon: ImageVector,
        val subtitle: String? = null,
        val onClick: () -> Unit
    ) : SettingsItem()
    
    /**
     * 可切换设置项（开关类型）
     */
    data class Toggleable(
        val title: String,
        val icon: ImageVector,
        val subtitle: String? = null,
        val initialValue: Boolean
    ) : SettingsItem()
} 